#pragma once
#include <map>

enum LINK_KEY {
	UID_KEY = 1,
	FD_KEY = 2
};

//网络句柄和用户ID之间的绑定关系
class SessionLinkPlayer
{
public:
	template<LINK_KEY T>
	int Find(const int key)
	{
		auto &dict = (T == UID_KEY) ? _uid_fd_dict : _fd_uid_dict;
		auto itr = dict.find(key);
		return (itr != dict.end()) ? itr->second : -1;
	}
	bool NewLink(const int fd, const int uid)
	{
		if (fd < 0 || uid < 0)
		{
			return false;
		}
		if (Find<UID_KEY>(uid) == -1 || Find<FD_KEY>(fd) == -1)
		{
			return false;
		}
		_uid_fd_dict[uid] = fd;
		_fd_uid_dict[fd] = uid;
		return true;
	}
	template<LINK_KEY T>
	bool Delete(int fd)
	{
		auto uid = Find<T>(fd);
		if (uid == -1)
		{
			return false;
		}
		return (1 == _fd_uid_dict.erase(fd) && 1 == _uid_fd_dict.erase(uid));
	}
private:
	std::map<int, int>	_fd_uid_dict;
	std::map<int, int>	_uid_fd_dict;
};